/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../provider/DataProvider.h"
#include "Player.h"
#include "MapReactors.h"
#include "../client/MapsData.h"
#include "../client/MapData.h"
#include "../client/ReactorData.h"
#include "../client/ReactorsData.h"
#include "../client/ReactorStateData.h"
#include "../client/MapReactorData.h"
#include "../client/MapReactorsData.h"
#include "Map.h"
#include "Item.h"
#include "Reactor.h"
#include "../tools/PacketCreator.h"
#include "../tools/Timer.h"
#include "../tools/Run.h"
#include "../provider/DataProvider.h"
#include "MapPlayers.h"
#include "MapDrops.h"
#include "../scripting/AngelScriptEngine.h"
#include "Drop.h"
#include "windows.h"

int MapReactors::ids = 0x200000;

class MapReactorsTimer : public Run {
private:
	MapReactors* map;
public:
	MapReactorsTimer(MapReactors* map):map(map){}
	void run(){
		map->checkSpawn(true);
	}
};

class ReactorTakeItemTimer : public Run {
private:
	Reactor* reactor;
	Drop* drop;
	Player* player;
public:
	ReactorTakeItemTimer(Reactor* reactor, Drop* drop, Player* player):reactor(reactor), drop(drop), player(player){}
	void run(){
		if(reactor->getMap()->getDrops()->findDrop(drop)){
			reactor->getMap()->getDrops()->removeDrop(drop);
			reactor->hit(reactor->getMap()->getPlayers()->find(player) ? player : NULL, 0, 0);
		}
	}

};

void MapReactors::takeItemTimer(Reactor* reactor, Drop* drop, Player* player) {
	if(reactor->getMap()->getDrops()->findDrop(drop) && reactor->inArea(drop->getPosition())) {
		reactor->getMap()->getDrops()->removeDrop(drop);
		reactor->hit(reactor->getMap()->getPlayers()->find(player) ? player : 0, 0, 0);
	}
}

MapReactors::MapReactors(Map* map) {
	this->map = map;
	timeron = false;
}

Reactor* MapReactors::getReactorByID(int objid) {
	for(int i=0; i<(int)reactors.size(); i++)
		if(reactors[i]->getID() == objid)
			return reactors[i];
	return 0;	
}

void MapReactors::show(Player* player) {
	for(int i=0; i<(int)reactors.size(); i++) {
		player->send(PacketCreator().spawnReactor(reactors[i]));
	}
}

void MapReactors::checkSpawn(bool timer) {
	if (map->getPlayers()->getPlayers()->empty())
	{
		timeron = false;
		return;
	}
	MapData* md = MapsData::getInstance()->getDataByID(map->getID());
	if(md == 0)
		return;
	vector <MapReactorData*>* sreactors = md->getReactorsData()->getData();
	for(int i=0; i<(int)sreactors->size(); i++) {
		bool spawned = false;
		for(int j=0; j<(int)reactors.size(); j++) {
			if((*sreactors)[i]->getInMapID() == reactors[j]->getOriginalID()) {
				spawned = true;
				break;
			}
		}	
		if(spawned)
			continue;
		if(last.find(i) == last.end())
			last[i] = 0;
		else if((*sreactors)[i]->getDelay() == -1)
			continue;
		else if(last[i] + (*sreactors)[i]->getDelay() * 1000 > (int)GetTickCount())
			continue;
		spawnReactor((*sreactors)[i]->getID(), (*sreactors)[i]->getX(), (*sreactors)[i]->getY(), (*sreactors)[i]->getInMapID());	
	}
	if (sreactors->size() > 0)
	{
		Timers::getInstance()->startTimer(10000, new MapReactorsTimer(this));
	}
}

void MapReactors::show(Reactor* reactor) {
	map->send(PacketCreator().spawnReactor(reactor));

}

void MapReactors::hitReactor(Reactor* reactor) {
	hitReactor(reactor, 0, 0, 0);

}

void MapReactors::hitReactor(Reactor* reactor, Player* player) {
	hitReactor(reactor, player, 0, 0);
}

void MapReactors::hitReactor(Reactor* reactor, Player* player, int stance, int pos) {
	reactor->hit(player, stance, pos);
	if(reactor->isDestroyed()) {
		destroy(reactor);
	}
}

void MapReactors::destroy(Reactor* reactor) {
	if(reactor->getOriginalID() != -1) {
		last[reactor->getOriginalID()] = GetTickCount();
	}
	map->send(PacketCreator().destroyReactor(reactor));
	reactors.erase(find(reactors.begin(), reactors.end(), reactor));
	delete reactor;
}

Reactor* MapReactors::spawnReactor(int reactorid, int x, int y, int orgID) {
	ReactorData* data = DataProvider::getInstance()->getReactorData(reactorid);
	if(data == 0)
		return 0;
	Reactor* reactor = new Reactor(map);
	reactor->setID(ids++);
	reactor->setReactorID(reactorid);
	Position pos;
	pos.x = x;
	pos.y = y;
	reactor->setPosition(pos);
	reactor->setOriginalID(orgID);
	map->send(PacketCreator().spawnReactor(reactor));
	reactors.push_back(reactor);
	return reactor;
}

Reactor* MapReactors::getReactorByReactorID(int id) {
	for(int i=0; i<(int)reactors.size(); i++) {
		if(reactors[i]->getReactorID() == id)
			return reactors[i];
	}
	return 0;
}

void MapReactors::checkDrop(Drop* drop, Player* player) {
	for(int i=0; i<(int)reactors.size(); i++) {
		ReactorData* rd = ReactorsData::getInstance()->getDataByID(reactors[i]->getReactorID()); 
		if(rd == 0)
			continue;
		ReactorStateData* s = rd->getDataByID(reactors[i]->getState());
		if(s == 0)
			continue;
		if(s->getType() == 100) { 
			if(s->getItemID() == drop->getItem()->getID() && drop->getItem()->getAmount() >= s->getAmount()) {
				if(reactors[i]->inArea(drop->getPosition())) {
					Timers::getInstance()->startTimer(5000, new ReactorTakeItemTimer(reactors[i], drop, player));
					break;
				}
			}
		}
	}
}

void MapReactors::clear() {
	for(int i=0; i<(int)reactors.size();) {
		destroy(reactors[0]);
	}
	last.clear();
	MapData* md = MapsData::getInstance()->getDataByID(map->getID());
	if(md == 0)
		return;
	vector <MapReactorData*>* sreactors = md->getReactorsData()->getData();
	for(int i=0; i<(int)sreactors->size(); i++) {
		spawnReactor((*sreactors)[i]->getID(), (*sreactors)[i]->getX(), (*sreactors)[i]->getY(), (*sreactors)[i]->getInMapID());	
	}
}